//
//  VisitorLoginView.swift
//  wmyweibo
//
//  Created by 王明玉 on 16/1/18.
//  Copyright © 2016年 王明玉. All rights reserved.
//

import UIKit

import SnapKit
//写一个协议 遵守根协议<NSObject> 如果不遵守NSObjectProtocol<基协议> 就无法使用weak的关键字
//声明可选的方法 需要optional关键字 因为是OC中的关键字 所以要在前面加一个@ @objc
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol{
    //协议方法
    //用户将要注册
    optional func userWillRegister()
    
    //用户将要登录
    optional func userWithLogin()
}


class VisitorLoginView: UIView {
    
    
    //写一个能让外界调用的方法
    func setupUIWithInfo(imageName: String?,tipText: String){
        if let img = imageName {
            circleIcon.image = UIImage(named: img)
            //隐藏大房子
            largeHouse.hidden = true
            bringSubviewToFront(circleIcon)
        }else{
            //说名在首页
            startAnimation()
        }
        tipLable.text = tipText
        
    }

    //圈圈的动画效果
    
    func startAnimation(){
        //基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        //设置重复次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        //持续时间
        base.duration = 10
        //设置动画完成 页面失去活跃状态 不移除图层
        base.removedOnCompletion = false
        //添加动画
        circleIcon.layer.addAnimation(base, forKey: nil)
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
   private func setupUI() {
    addSubview(circleIcon)
    addSubview(backview)
    
    addSubview(largeHouse)
    addSubview(tipLable)
    addSubview(loginBtn)
    addSubview(registerBtn)
    
    //设置自动布局 苹果手码布局通过 vfl
    //snp_makeConstrants 表示添加一条约束
    circleIcon.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(self.snp_centerX)
        make.centerY.equalTo(self.snp_centerY)
    }
    largeHouse.snp_makeConstraints { (make) -> Void in
        make.center.equalTo(self.snp_center)
    }
    tipLable.snp_makeConstraints { (make) -> Void in
        make.centerX.equalTo(circleIcon)
        make.top.equalTo(circleIcon.snp_bottom).offset(16)
        //宽度的约束
        make.width.equalTo(225)
        //防止页面跳动
        make.height.equalTo(40)
    }
    //登录按钮
    loginBtn.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(tipLable.snp_left)
        make.top.equalTo(tipLable.snp_bottom).offset(16)
        make.width.equalTo(100)
    }
    //注册按钮
    registerBtn.snp_makeConstraints { (make) -> Void in
        make.right.equalTo(tipLable.snp_right)
        make.top.equalTo(tipLable.snp_bottom).offset(16)
        make.width.equalTo(100)
    }
    backview.snp_makeConstraints { (make) -> Void in
//        make.top.equalTo(self.snp_top)
//        make.left.equalTo(self.snp_left)
//        make.right.equalTo(self.snp_right)
        //和Masory 类似 支持链式设置约束模式
        make.top.left.right.equalTo(self)
        make.bottom.equalTo(loginBtn.snp_bottom)
    }
    //设置灰度
    self.backgroundColor = UIColor(white: 0.93, alpha: 1)
    }
    
    
    
    
    //懒加载子视图
    //背景
    lazy var backview : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示图案
    lazy var tipLable: UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(14)
        l.text = "登录后，别人会评论你的微薄"
        l.textColor = UIColor.darkGrayColor()
        l.textAlignment = .Center
        l.numberOfLines = 0
        //设置大小
        l.sizeToFit()
        return l
    }()
    //登录按钮
    lazy var loginBtn: UIButton = {
       let btn = UIButton()
        //设置按钮文字
        btn.setTitle("登录", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        return btn
    }()
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        //设置按钮文字
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return btn
    }()
    
    
}
